package com.dxwd.watermonitoring;

import android.app.Fragment;
import android.app.FragmentManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainFragmentActivity extends FragmentActivity {
	private static TextView remind_tv;	
	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);

		FragmentManager fm = getFragmentManager();
		if (fm.findFragmentById(android.R.id.content) == null) {
			MainFragment tab0Fragment = new MainFragment();
			fm.beginTransaction().add(android.R.id.content, tab0Fragment)
					.commit();
		}
		
	}

	
	public static class MainFragment extends Fragment {
		private String TAG="MainFragment";
		private MainFragmentBroadcastReceiver receiver;
		private IntentFilter intentFilter;
		
		private Button btn_help,btn_about;
		

		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			Log.i(TAG,"create");
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View v = inflater.inflate(R.layout.tab0, container, false);
			btn_help = (Button) v.findViewById(R.id.button1);
			btn_about = (Button) v.findViewById(R.id.button2);
			remind_tv = (TextView) v.findViewById(R.id.textView2);
			//textView2.setText("TextView8");
			btn_about.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Toast.makeText(getActivity(), "about", Toast.LENGTH_SHORT).show();
				}
			});
			btn_help.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Toast.makeText(getActivity(), "help", Toast.LENGTH_SHORT).show();
				}
			});
			remind_tv.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					
                    Intent intent = new Intent();
                    //intent.setClass(getActivity(), Main.class);
                    intent.setAction("MAIN_ACTION");
                    intent.putExtra("tab", 1);  
                    getActivity().sendBroadcast(intent);
                   // startActivity(intent);
                   // getActivity().finish();
					
					Log.i("MainFragemnt","remind is clicked!");
                    
				}
			});
			return v;
		}

		@Override
		public void onPause() {
			super.onPause();
			getActivity().unregisterReceiver(receiver);			
			Log.i(TAG,"pause");
			
		}

		@Override
		public void onResume() {

			super.onResume();
			receiver = new MainFragmentBroadcastReceiver();
			getActivity().registerReceiver(receiver, getIntentFilter());
			if(MonitoringInfo.getRemindString() != null && MonitoringInfo.getRemindString() != remind_tv.getText().toString()){
				remind_tv.setText(MonitoringInfo.getRemindString());
				remind_tv.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
			}
			Log.i(TAG,"resume");
		}

		private IntentFilter getIntentFilter() {
			if (intentFilter == null) {
				intentFilter = new IntentFilter();
				intentFilter.addAction("TAB_ACTION");
			}
			return intentFilter;
		}

		class MainFragmentBroadcastReceiver extends BroadcastReceiver {

			

			@Override
			public void onReceive(Context context, Intent intent) {
				if (intent.getAction().equals("TAB_ACTION")) {
					//textView1.setText("Received!");
					Log.i(TAG,"tab0 on received broadcast:"+intent.getIntExtra("i", 0));
					if(MonitoringInfo.getRemindString() != remind_tv.getText().toString()){						
						remind_tv.setText(MonitoringInfo.getRemindString());
						if(MonitoringInfo.getRemindString() == getResources().getString(R.string.normal_range)){
							remind_tv.setTextColor(getResources().getColor(android.R.color.black));
						}else{
							remind_tv.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
						}
					}
					
				
				}
			}

		}
	}
}
